#include "EventConfig.h"

#include <object_v2/object_v2.h>

struct _EventConfig
{
        GObject       parent_instance;

        ObjectString *config_path_name;

        /*用户信息*/
        ObjectString *user_class;
        ObjectString *user_name;
        ObjectString *user_password;

        /*是否包含了基本用户信息*/
        gboolean is_contain_user_info;
};

G_DEFINE_FINAL_TYPE(EventConfig, event_config, G_TYPE_OBJECT)

static void
event_config_finalize(GObject *object)
{
        EventConfig *self = EVENT_CONFIG(object);

        object_unref(self->config_path_name);
        object_unref(self->user_class);
        object_unref(self->user_name);
        object_unref(self->user_password);

        G_OBJECT_CLASS(event_config_parent_class)->finalize(object);

        OBJECT_INFO_TOOL(EventConfig, finalize, "已清理\n");
}

static void
event_config_class_init(EventConfigClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS(klass);

        object_class->finalize     = event_config_finalize;
}

EventConfig *
event_config_new()
{
        return g_object_new(EVENT_TYPE_CONFIG, NULL);
}

gboolean
event_config_read_config(EventConfig *self)
{
        GError *error = NULL;
        GFile  *fp;
        fp             = g_file_new_for_path(self->config_path_name->charset);

        char *contents = NULL;
        gsize length   = 0;
        if (!g_file_query_exists(fp, NULL)) {
                OBJECT_INFO_TOOL(EventConfig,
                                 read_config,
                                 "将创建配置文件 '%s'\n",
                                 self->config_path_name->charset);
                GFileOutputStream *output_stream;
                output_stream =
                        g_file_create(fp, G_FILE_CREATE_NONE, NULL, &error);
                g_object_unref(output_stream);
        } else {
                g_file_load_contents(fp,
                                     NULL,
                                     &contents,
                                     &length,
                                     NULL,
                                     &error);
        }
        g_object_unref(fp);

        if (error) {
                OBJECT_ERROR_TOOL(EventConfig,
                                  read_config,
                                  "文件操作失败！%s\n",
                                  error->message);
                return false;
        }

        if (length == 0) {
                OBJECT_INFO_TOOL(EventConfig, read_config, "文件无内容可读\n");
                return false;
        }

        ObjectStringApplication *str_app =
                object_string_application_new_with(contents);
        g_free(contents);
        object_string_application_set_segment_charset(str_app, '\n');

        ObjectStringApplication *line, *tmp;
        object_string_application_rewind(str_app);

retry:
        line = object_string_application_get_string_before_segment(str_app);
        if (!line) {
                self->is_contain_user_info = true;
                OBJECT_INFO_TOOL(EventConfig,
                                 read_config,
                                 "载入了用户信息: %s%s, <%s>\n",
                                 self->user_class->charset,
                                 self->user_name->charset,
                                 self->user_password->charset);
                object_unref(str_app);
                return true;
        }

        object_string_application_set_segment_charset(line, '=');
        object_string_application_rewind(line);

        tmp = object_string_application_get_string_before_segment(line);

        if (object_string_compare_with_charset(OBJECT_STRING(tmp),
                                               "userName")) {
                object_unref(tmp);
                tmp = object_string_application_get_string_before_segment(line);
                if (tmp) {
                        object_string_set_string(
                                self->user_name,
                                object_string_get_string(OBJECT_STRING(tmp)));
                }
        } else if (object_string_compare_with_charset(OBJECT_STRING(tmp),
                                                      "userClass")) {
                object_unref(tmp);
                tmp = object_string_application_get_string_before_segment(line);
                if (tmp) {
                        object_string_set_string(
                                self->user_class,
                                object_string_get_string(OBJECT_STRING(tmp)));
                }
        } else if (object_string_compare_with_charset(OBJECT_STRING(tmp),
                                                      "userPassword")) {
                object_unref(tmp);
                tmp = object_string_application_get_string_before_segment(line);
                if (tmp) {
                        object_string_set_string(
                                self->user_password,
                                object_string_get_string(OBJECT_STRING(tmp)));
                }
        }

        object_unref(tmp);
        object_unref(line);
        goto retry;
}

void
event_config_set_user_info(EventConfig *self,
                           const char  *user_class,
                           const char  *user_name,
                           const char  *user_password)
{
        if (!user_class || !user_name || !user_password) {
                OBJECT_ERROR_TOOL(EventConfig,
                                  set_user_info,
                                  "捕获到空参数！<%p><%p><%p>\n",
                                  user_class,
                                  user_name,
                                  user_password);
                return;
        }

        object_string_set_string(self->user_class, user_class);
        object_string_set_string(self->user_name, user_name);
        object_string_set_string(self->user_password, user_password);
}

void
event_config_write_config(EventConfig *self)
{
        GError        *error = NULL;
        GFile         *fp;
        GOutputStream *ostream;
        GFileIOStream *iofp;

        fp = g_file_new_for_path(self->config_path_name->charset);

        /*文件存在的情况*/
        if (g_file_query_exists(fp, NULL)) {
                iofp = g_file_open_readwrite(fp, NULL, &error);
                if (error) {
                        OBJECT_ERROR_TOOL(EventConfig,
                                          write_config,
                                          "创建文件失败！%s\n",
                                          error->message);
                        if (iofp)
                                g_object_unref(iofp);
                        g_object_unref(fp);
                        return;
                }
                ostream = g_io_stream_get_output_stream(G_IO_STREAM(iofp));
        } else {
                ostream = G_OUTPUT_STREAM(
                        g_file_create(fp, G_FILE_CREATE_NONE, NULL, &error));
        }

        if (error) {
                OBJECT_ERROR_TOOL(EventConfig,
                                  write_config,
                                  "创建文件失败！%s\n",
                                  error->message);
                if (ostream)
                        g_object_unref(ostream);
                g_object_unref(iofp);
                g_object_unref(fp);
                return;
        }

        ObjectString *output_content = object_string_new();
        object_string_append_string(output_content, "userClass=");
        object_string_append_string(output_content, self->user_class->charset);
        object_string_append_string(output_content, "\nuserName=");
        object_string_append_string(output_content, self->user_name->charset);
        object_string_append_string(output_content, "\nuserPassword=");
        object_string_append_string(output_content,
                                    self->user_password->charset);

        g_output_stream_write(ostream,
                              output_content->charset,
                              output_content->length,
                              NULL,
                              &error);

        if (error) {
                OBJECT_ERROR_TOOL(EventConfig,
                                  write_config,
                                  "写文件流失败！%s\n",
                                  error->message);
        }

        object_unref(output_content);
        /*
        g_output_stream_close (ostream, NULL, &error);
        if (error) {
                OBJECT_WARNING_TOOL (EventConfig,
                                     write_config,
                                     "关闭文件流错误！%s\n",
                                     error->message);
        }
        */
        g_object_unref(ostream);
        g_object_unref(iofp);
        g_object_unref(fp);
        return;
}

ObjectString *
event_config_reference_user_class(EventConfig *self)
{
        return object_reference_to(self->user_class, OBJECT_STRING);
}

ObjectString *
event_config_reference_user_name(EventConfig *self)
{
        return object_reference_to(self->user_name, OBJECT_STRING);
}

ObjectString *
event_config_reference_user_password(EventConfig *self)
{
        return object_reference_to(self->user_password, OBJECT_STRING);
}

static void
event_config_init(EventConfig *self)
{
        char *home_env;

        self->config_path_name = object_string_new();

        /*获取 HOME PATH */
        home_env = getenv("HOME");

        if (!home_env) {
                OBJECT_ERROR_TOOL(EventConfig,
                                  init,
                                  "无法获取 HOME 环境变量！\n");
                object_string_append_string(self->config_path_name, "./");
        } else {
                object_string_append_string(self->config_path_name, home_env);
                object_string_append_string(self->config_path_name,
                                            EVENT_CONFIG_DEFAULT_PATH);
        }

        object_string_append_string(self->config_path_name, EVENT_CONFIG_NAME);

        OBJECT_INFO_TOOL(EventConfig,
                         init,
                         "拟获取配置文件路径 '%s'\n",
                         self->config_path_name->charset);

        self->user_class           = object_string_new();
        self->user_name            = object_string_new();
        self->user_password        = object_string_new();
        self->is_contain_user_info = false;
}